layui.define(['table', 'form', 'laypage', 'laydate', 'layer', 'carousel'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , laydate = layui.laydate
        , laypage = layui.laypage
        , layer = layui.layer
        , carousel = layui.carousel;

    //路径
    var curWwwPath = window.document.location.href;
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    var localhostPath = curWwwPath.substring(0, pos);
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);

    var limitcount = 10;
    table.render({
        elem: '#alertValueTable'
        , url: localhostPath + projectName + "/rest/system/getAlertValueList"
        , toolbar: '#alertValueOperation'
        , height: 'full-200'
        , title: '预警参数表'
        , defaultToolbar: ['filter', 'print']
        , cols: [[
            {field: 'zhen',title:"镇", sort: true, fixed: 'left',},
            {field: 'cun',  title: '村', sort: true},
            {field: 'csbStart', title: '产水泵开始时间(分钟)', sort: true},
            {field: 'csbEnd', title: '产水泵结束时间(分钟)', sort: true},
            {field: 'fxbStart', title: '反洗泵开始时间(分钟)', sort: true},
            {field: 'fxbEnd', title: '反洗泵结束时间(分钟)', sort: true},
            {field: 'gfjStart', title: '鼓风机开始时间(分钟)', sort: true},
            {field: 'gfjEnd', title: '鼓风机结束时间(分钟)', sort: true},
            {field: 'jbjStart', title: '搅拌机开始时间(分钟)', sort: true},
            {field: 'jbjEnd', title: '搅拌机结束时间(分钟)', sort: true},
            {field: 'hlbStart', title: '回流泵开始时间(分钟)', sort: true},
            {field: 'hlbEnd', title: '回流泵结束时间(分钟)', sort: true}
            // {title: '操作', toolbar: '#table-alertValue', align: "center", width: 250, fixed: "right"}
            ]]
        , page: false
        , limit: limitcount
        , text: {
            none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
        },
        // limits:[10, 20, 30, 40, 50],
        done: function (res, curr, count) {
            if (res.errorMsg == null) {

            } else {
                layer.msg(res.errorMsg);
            }
            var curnum = $("#curnum").val();
            $('.layui-table-fixed-r').removeClass('layui-hide');//列固定右侧
            laypage.render({
                elem: 'laypage'
                , first: '首页'
                , last: '尾页'
                , groups: 5
                , count: count
                , curr: curnum
                , limit: limitcount
                , layout: ['prev', 'page', 'next', 'skip', 'count', 'limit']
                , limits: [10, 15, 20]
                , jump: function (obj, first) {
                    if (!first) {
                        curnum = obj.curr;
                        $("#curnum").val(curnum)
                        limitcount = obj.limit;
                        table.reload('alertValueTable', { //表格的id
                            where: {
                                page: curnum,
                                limit: limitcount
                            }
                        })

                    }
                }
            })
        }
    });

    //监听工具条
    table.on('tool(alertValueTables)', function (obj) {  //表格的lay-filter="userlists"
        var data = obj.data; //获得当前行所有数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        if (obj.event === 'del') {
            deletes(data, obj);
        } else if (obj.event === 'update') {
            update(data);
        } else if (obj.event === 'info') {
            info(data)
        }
    });

    function deletes(data, obj) {
        layer.confirm('确定删除此数据', {icon: 3, title: '提示信息'}, function (index) {
            var index = layer.msg('删除中，请稍候', {icon: 16, time: false, shade: 0.6});
            setTimeout(function () {
                //向服务端发送删除指令
                $.ajax({
                    type: "post",
                    url: localhostPath + projectName + "/rest/base/delAlertValue",
                    data: "selectdata=" + data.tid,
                    dataType: "json",
                    async: false,
                    success: function (msg) {
                        layer.close(index);
                        if (msg === true) {
                            layer.msg('删除成功');
                            obj.del();
                        } else {
                            layer.msg(msg.errorMsg);
                        }
                    }
                })
            }, 2000);
        });
    }

    function update(data) {
        var clocess = "1";
        layer.open({
            title: "修改",
            type: 2,
            content: localhostPath + projectName + '/rest/getRoute?type=system&url=alertvalue_update',
            //是否显示右上角关闭图标，默认为1，显示
            area: ['90%', '91%'],
            maxmin: true,
            shade: 0.6,
            anim: 4,
            skin: 'layui-layer-molv',
            resize: true,
            success: function (layero, index) {  //当你需要在层创建完毕时即执行一些语句，可以通过该回调 想相当于吧值转到子界面
                var iframe = window['layui-layer-iframe' + index];
                iframe.child(data.tid)
            }, cancel: function (index, layero) {
                layer.close(index)
                clocess = "2"
                return false;
            },
            end: function () {
                if (clocess !== "2") {
                    table.reload('alertValueTable')
                }
                return false;
            }
        });
    }

    function info(data) {
        layer.open({
            title: "详情",
            type: 2,
            content: localhostPath + projectName + '/rest/getRoute?type=system&url=alertvalue_info',
            //是否显示右上角关闭图标，默认为1，显示
            area: ['90%', '91%'],
            maxmin: true,
            shade: 0.6,
            anim: 4,
            skin: 'layui-layer-molv',
            resize: true,
            success: function (layero, index) {  //当你需要在层创建完毕时即执行一些语句，可以通过该回调 想相当于吧值转到子界面
                var iframe = window['layui-layer-iframe' + index];
                iframe.child(data.tid)
            }
        });
    }
    exports('alertValue', {})
    }
)
;